/**
 * @Author: ls <ls-mac>
 * @Date:   2019-08-11 17:38:31
 * @Last modified by:   ls-mac
 * @Last modified time: 2019-10-10 23:22:02
 */

import { companyStorePage, list, getCompanyStoreList, companyStoreAdd } from 'services/enterprise';

export default {
  namespace: 'companyStorePage',
  state: {
    data: {
      list: [],
      pagination: {},
    },
    companyStorePage: [],
  },

  effects: {
    *companyStorePage({ payload, callback }, { call, put }) {
      const response = yield call(companyStorePage, payload);
      yield put({
        type: 'saveList',
        payload: response,
      });
      if (callback) callback();
    },

    // 企业信息
    *companyList({ payload, callback }, { call }) {
      const response = yield call(list, payload);
      if (callback) callback(response);
    },

    // 已选门店
    *getCompanyStoreList({ payload, callback }, { call }) {
      const response = yield call(getCompanyStoreList, payload);
      if (callback) callback(response);
    },

    // 提交
    *companyStoreAdd({ payload, callback }, { call }) {
      const response = yield call(companyStoreAdd, payload);
      if (callback) callback(response);
    },
  },

  reducers: {
    saveList(state, action) {
      const {
        data: { companyStoreList, pageIndex, pageSize, recordCount },
      } = action.payload;

      const newList = companyStoreList.map(val => {
        const v = val;
        v.key = `k${v.id}`;
        return v;
      });

      return {
        ...state,
        data: {
          list: newList,
          pagination: {
            current: pageIndex,
            total: recordCount,
            pageSize,
          },
        },
      };
    },
  },
};
